
import { ControllerBase } from "@/components";
import { IMenu } from "./interfaces/IMenu";
import React from "react";

export type { IMenu } from './interfaces/IMenu';

class State {
    menu: IMenu[] = []
}

/** 菜单控制器 */
export class MenuController extends ControllerBase<State> {

    static Context = React.createContext<MenuController>(undefined as any)

    static State = State;

    /** 获取孩子 */
    static getChildren = (id: string, menus: IMenu[]) => {
        return menus.filter(m => m.menuParentId === id)
    }

    constructor() {
        super(new State())
    }

    /** 查询菜单 */
    queryMenu = this.action(async s => {
        try {
            const url = `${import.meta.env.BASE_URL}config/menu.json`;
            const res = await fetch(url);
            s.menu = JSON.parse(await res.text())
        } catch (error) {
            s.menu = []
        }
    })
}


